home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZFONT.C < prev    next >
C/C++ Source or Header  |  1991-12-03  |  8KB  |  278 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zfont.c */
  21. /* Font operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. /*
  26.  * The following lines used to say:
  27.  *    #include "gsmatrix.h"
  28.  *    #include "gxdevice.h"        /. for gxfont.h ./
  29.  * Tony Li says the longer list is necessary to keep the GNU compiler
  30.  * happy, but this is pretty hard to understand....
  31.  */
  32. #include    "gxfixed.h"
  33. #include    "gxmatrix.h"
  34. #include    "gzstate.h"        /* must precede gxdevice */
  35. #include    "gxdevice.h"        /* must precede gxfont */
  36. #include    "gschar.h"
  37. #include "gxfont.h"
  38. #include "gxfdir.h"
  39. #include "alloc.h"
  40. #include "font.h"
  41. #include "dict.h"
  42. #include "name.h"
  43. #include "packed.h"
  44. #include "save.h"
  45. #include "state.h"
  46. #include "store.h"
  47.  
  48. /* Imported operators */
  49. extern int zcleartomark(P1(os_ptr));
  50.  
  51. /* Forward references */
  52. private int font_param(P2(os_ptr, gs_font **));
  53. int add_FID(P2(ref *, gs_font *));    /* needed for buildfont */
  54. private int make_font(P2(os_ptr, gs_matrix *));
  55. private void make_uint_array(P3(os_ptr, uint *, int));
  56.  
  57. /* The (global) font directory */
  58. gs_font_dir *ifont_dir;            /* needed for buildfont */
  59.  
  60. /* Global font-related objects */
  61. ref name_StandardEncoding;        /* only needed for seac */
  62. /* Names of system-known keys in font dictionaries: */
  63. ref name_FontMatrix;            /* needed for buildfont */
  64. ref name_FID;
  65.  
  66. /* Initialize the font operators */
  67. private void
  68. zfont_init()
  69. {    static names_def fnd[] = {
  70.  
  71.     /* Create the names of the global known objects. */
  72.        { "StandardEncoding", &name_StandardEncoding },
  73.  
  74.     /* Create the names of the standard elements of */
  75.     /* a font dictionary. */
  76.        { "FontMatrix", &name_FontMatrix },
  77.        { "FID", &name_FID },
  78.  
  79.     /* Mark the end of the initalized name list. */
  80.        names_def_end
  81.     };
  82.  
  83.     ifont_dir = gs_font_dir_alloc(alloc, alloc_free);
  84.     init_names(fnd);
  85. }
  86.  
  87. /* scalefont */
  88. int
  89. zscalefont(register os_ptr op)
  90. {    int code;
  91.     float scale;
  92.     gs_matrix mat;
  93.     if ( (code = num_params(op, 1, &scale)) < 0 ) return code;
  94.     if ( (code = gs_make_scaling(scale, scale, &mat)) < 0 ) return code;
  95.     return make_font(op, &mat);
  96. }
  97.  
  98. /* makefont */
  99. int
  100. zmakefont(register os_ptr op)
  101. {    int code;
  102.     gs_matrix mat;
  103.     if ( (code = read_matrix(op, &mat)) < 0 ) return code;
  104.     return make_font(op, &mat);
  105. }
  106.  
  107. /* setfont */
  108. int
  109. zsetfont(register os_ptr op)
  110. {    gs_font *pfont;
  111.     int code = font_param(op, &pfont);
  112.     if ( code < 0 || (code = gs_setfont(igs, pfont)) < 0 )
  113.       return code;
  114.     istate.font = *op;
  115.     pop(1);
  116.     return code;
  117. }
  118.  
  119. /* currentfont */
  120. int
  121. zcurrentfont(register os_ptr op)
  122. {    push(1);
  123.     *op = istate.font;
  124.     return 0;
  125. }
  126.  
  127. /* cachestatus */
  128. int
  129. zcachestatus(register os_ptr op)
  130. {    uint status[7];
  131.     gs_cachestatus(ifont_dir, status);
  132.     push(7);
  133.     make_uint_array(op - 6, status, 7);
  134.     return 0;
  135. }
  136.  
  137. /* setcachelimit */
  138. int
  139. zsetcachelimit(register os_ptr op)
  140. {    long limit;
  141.     check_type(*op, t_integer);
  142.     limit = op->value.intval;
  143.     if ( (ulong)limit > max_uint )    /* also covers limit < 0 */
  144.         return e_rangecheck;
  145.     gs_setcachelimit(ifont_dir, (uint)limit);
  146.     pop(1);
  147.     return 0;
  148. }
  149.  
  150. /* setcacheparams */
  151. int
  152. zsetcacheparams(register os_ptr op)
  153. {    uint params[2];
  154.     int i, code;
  155.     for ( i = 0; i < 2 && !r_has_type(op - i, t_mark) ; i++ )
  156.        {    long parm;
  157.         check_type(op[-i], t_integer);
  158.         parm = op[-i].value.intval;
  159.         if ( (ulong)parm > max_uint )    /* covers parm < 0 */
  160.             return e_rangecheck;
  161.         params[i] = parm;
  162.        }
  163.     switch ( i )
  164.        {
  165.     case 2:
  166.         if ( (code = gs_setcachelower(ifont_dir, params[1])) < 0 )
  167.             return code;
  168.     case 1:
  169.         if ( (code = gs_setcacheupper(ifont_dir, params[0])) < 0 )
  170.             return code;
  171.     case 0: ;
  172.        }
  173.     return zcleartomark(op);
  174. }
  175.  
  176. /* currentcacheparams */
  177. int
  178. zcurrentcacheparams(register os_ptr op)
  179. {    uint params[2];
  180.     params[0] = gs_currentcachelower(ifont_dir);
  181.     params[1] = gs_currentcacheupper(ifont_dir);
  182.     push(3);
  183.     make_tv(op - 2, t_mark, intval, 0);
  184.     make_uint_array(op - 1, params, 2);
  185.     return 0;
  186. }
  187.  
  188. /* ------ Initialization procedure ------ */
  189.  
  190. op_def zfont_op_defs[] = {
  191.     {"0currentfont", zcurrentfont},
  192.     {"2makefont", zmakefont},
  193.     {"2scalefont", zscalefont},
  194.     {"1setfont", zsetfont},
  195.     {"0cachestatus", zcachestatus},
  196.     {"1setcachelimit", zsetcachelimit},
  197.     {"1setcacheparams", zsetcacheparams},
  198.     {"0currentcacheparams", zcurrentcacheparams},
  199.     op_def_end(zfont_init)
  200. };
  201.  
  202. /* ------ Subroutines ------ */
  203.  
  204. /* Validate a font parameter. */
  205. private int
  206. font_param(os_ptr fp, gs_font **pfont)
  207. {    /* Check that fp is a read-only dictionary, */
  208.     /* and that it has a FID entry. */
  209.     ref *pid;
  210.     int code;
  211.     check_type(*fp, t_dictionary);
  212.     if ( (code = dict_find(fp, &name_FID, &pid)) < 0 ) return code;
  213.     *pfont = pid->value.pfont;
  214.     if ( *pfont == 0 ) return e_invalidfont; /* unregistered font */
  215.     return 0;
  216. }
  217.  
  218. /* Add the FID entry to a font dictionary. */
  219. int
  220. add_FID(ref *fp /* t_dictionary */,  gs_font *pfont)
  221. {    ref fid;
  222.     make_tv_new(&fid, t_fontID, pfont, pfont);
  223.     return dict_put(fp, &name_FID, &fid);
  224. }
  225.  
  226. /* Make a transformed font (common code for makefont/scalefont). */
  227. private int
  228. make_font(os_ptr op, gs_matrix *pmat)
  229. {    os_ptr fp = op - 1;
  230.     gs_font *oldfont, *newfont, *ffont;
  231.     ref newdict;
  232.     ref newmat;
  233.     ref *mbody;
  234.     int code;
  235.     if ( (code = font_param(fp, &oldfont)) < 0 ||
  236.          (code = gs_makefont(ifont_dir, oldfont, pmat,
  237.                  &newfont, &ffont)) < 0 ||
  238.          (code = dict_create(dict_maxlength(fp), &newdict)) < 0 ||
  239.          (code = dict_copy(fp, &newdict)) < 0 ||
  240.          (code = ((mbody = alloc_refs(6, "make_font")) == 0 ? e_VMerror : 0)) < 0 ||
  241.          (make_tasv_new(&newmat, t_array, a_all, 6, refs, mbody),
  242.           (code = dict_put(&newdict, &name_FontMatrix, &newmat))) < 0 ||
  243.          (code = add_FID(&newdict, newfont)) < 0
  244.        )
  245.       return code;
  246.     *(gs_matrix *)mbody = newfont->FontMatrix;
  247.     if ( ffont )
  248.       { /****** SHOULD DECREMENT REFCT ******/
  249.       }
  250.     r_clear_attrs(dict_access_ref(&newdict), a_write);
  251.     *fp = newdict;
  252.     pop(1);
  253.     return 0;
  254. }
  255.  
  256. /* Convert an array of (unsigned) integers to stack form. */
  257. private void
  258. make_uint_array(register os_ptr op, uint *intp, int count)
  259. {    int i;
  260.     for ( i = 0; i < count; i++, op++, intp++ )
  261.         make_int(op, *intp);
  262. }
  263.  
  264. /* Remove scaled font and character cache entries that would be */
  265. /* invalidated by a restore. */
  266. extern void gs_purge_font_from_caches(P2(gs_font_dir *, gs_font *));
  267. void
  268. font_restore(alloc_save *save)
  269. {    gs_font_dir *pdir = ifont_dir;
  270.     gs_font *pfont;
  271. top:    for ( pfont = pdir->scaled_fonts; pfont != 0; pfont = pfont->next )
  272.       { if ( alloc_is_since_save((char *)pfont, save) )
  273.           { gs_purge_font_from_caches(pdir, pfont); goto top; }
  274.         else if ( alloc_is_since_save((char *)pfont->base, save) )
  275.           { gs_purge_font_from_caches(pdir, pfont->base); goto top; }
  276.       }
  277. }
  278.